/*!
\copyright  Copyright (c) 2024 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file       anc_test.c
\brief      Implementation of ANC testing functions.
*/

#ifdef INCLUDE_ANC_V3

#include "anc_audio_curation.h"
#include "audio_curation_objective.h"
#include "audio_curation_intent.h"
#include "audio_curation_objective_message.h"
#include "audio_curation_objective_private.h"
#include "audio_curation_router.h"
#include "audio_curation.h"
#include "phy_state.h"
#include "context_framework.h"
#include "ui_inputs.h"
#include "ui.h"
#include "peer_ui.h"
#include "message.h"
#include "logging.h"
#include "audio_curation_objective_tuning.h"
#include "audio_curation_objective_transparency.h"
#include "audio_curation_monitor.h"
#include "audio_curation_hearingtest.h"
#include "audio_curation_usecase_manager.h"
#include "multidevice.h"
#include "peer_signalling.h"
#include "operators.h"

#define OPID_MAX_NUM 50
#define OPID_NOT_FOUND 0xFFFF

uint16 appTestAudioCuration_GetOpId(uint16 cap_id, uint16 inst)
{
    OperatorGetInfo op_info[OPID_MAX_NUM];
    uint16 n_info = 0;
    uint16 opid_found = OPID_NOT_FOUND;
    uint16 inst_cnt = inst;

    OperatorGetInfoMultiple(0, op_info, OPID_MAX_NUM, &n_info);
    for (int16 op = 0; op < n_info; op++)
    {
        if (op_info[op].cap_id == cap_id)
        {
            if (inst_cnt == 0)
            {
                opid_found = op_info[op].opid;
                break;
            }
            else
            {
                inst_cnt--;
            }
        }
    }
    if (opid_found == OPID_NOT_FOUND)
    {
        DEBUG_LOG_ERROR("appTestAudioCuration_GetOpId: Inst %d of cap_id 0x%x not found. Graph has %d operators", inst, cap_id, n_info);
    }
    else
    {
        DEBUG_LOG_ALWAYS("appTestAudioCuration_GetOpId: Inst %d of cap_id 0x%x -> opid 0x%x", inst, cap_id, opid_found);
    }
    return opid_found;
}


UNITCODESECTION(KEEP)
void appTestAudioCuration_SetAncEnable(void)
{
    if (appPhyStateIsOutOfCase())
    {
        AudioCurationMesssage_SetAncEnable();
    }
}

UNITCODESECTION(KEEP)
void appTestAudioCuration_SetAncDisable(void)
{
    if (appPhyStateIsOutOfCase())
    {
        AudioCurationMesssage_SetAncDisable();
    }
}

UNITCODESECTION(KEEP)
void appTestAudioCuration_SetMode(uint8 req_mode)
{
    if (appPhyStateIsOutOfCase())
    {
        AudioCurationMesssage_SetMode(req_mode);
    }
}

UNITCODESECTION(KEEP)
bool appTestAudioCuration_EnableTuning(bool enable)
{
    DEBUG_LOG_ALWAYS("appTestAudioCuration_EnableTuning: %d", enable);
    return AudioCurationObjectiveTuning_Enable(enable);
}

UNITCODESECTION(KEEP)
void appTestAudioCuration_EnterTuning(uint8 mode)
{
    AudioCurationMesssage_EnterTuning(mode);
}

UNITCODESECTION(KEEP)
void appTestAudioCuration_ExitTuning(void)
{
    AudioCurationMesssage_ExitTuning( );
}

UNITCODESECTION(KEEP)
uint8 appTestAudioCuration_GetCurrentMode(void)
{
    DEBUG_LOG_ALWAYS("appTestAudioCuration_GetCurrentMode");
    return AudioCurationObjective_GetCurrentObjective();
}

UNITCODESECTION(KEEP)
bool appTestAudioCuration_ReadCapParam(uint16 cap_id, uint16 inst, uint32_t pid, uint32_t param_size, uint8_t* param)
{
    bool valid = FALSE;
    Operator opid = appTestAudioCuration_GetOpId(cap_id, inst);

    DEBUG_LOG_ALWAYS("appTestAudioCuration_ReadCapParam cap_id 0x%x pid 0x%x size %d [bytes]", cap_id, pid, param_size);

    if (opid != OPID_NOT_FOUND)
    {
        valid = OperatorsGetH2XmlParam(opid, pid, param_size, param);
    }
    return valid;
}

UNITCODESECTION(KEEP)
bool appTestAudioCuration_WriteCapParam(uint16 cap_id, uint16 inst, uint32_t pid, uint32_t param_size, uint8_t* param)
{
    bool valid = FALSE;
    Operator opid = appTestAudioCuration_GetOpId(cap_id, inst);

    DEBUG_LOG_ALWAYS("appTestAudioCuration_WriteCapParam cap_id 0x%x pid 0x%x size %d [bytes]", cap_id, pid, param_size);

    if (opid != OPID_NOT_FOUND)
    {
        valid = OperatorsSetH2XmlParam(opid, pid, param_size, param);
    }
    return valid;
}

bool appTestAudioCuration_SetCalibration(uint32 param_size, uint8* param, bool permanent)
{
    return (AudioCurationRouter_SetCalibration(param_size, param, permanent));
}

#endif /* INCLUDE_ANC_V3 */
